<?php

namespace App\User\Http\Controller\Wap;

use App\ApiBaseController;
use App\Im\Services\ImService;
use App\User\Models\User;
use App\WebSocket\Services\DeviceService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use Illuminate\Support\Facades\Validator;
use App\Common\Services\UserTokenService;
use App\Handles\CrmRpcHandle;
use Jenssegers\Agent\Agent;
use App\Common\Services\SmsService;

class UserController extends ApiBaseController
{
    use CrmRpcHandle;

    /**
     * 表单验证
     * @param $data
     * @param $type
     * @return bool
     * @throws GoodsException
     */
    protected function validate($data, $ruleArr, $source = '')
    {
        $rule = [
            'phone' => ['required', 'regex:/^((\(\d{2,3}\))|(\d{3}\-))?1(3|5|8|9)\d{9}$/'],
            'code' => 'required|integer',
            'uuid' => 'required|string'
        ];

        $message = [
            'phone.required' => '请输入手机号',
            'phone.regex' => '手机号格式异常',
            'code.required' => '请输入验证码',
            'code.integer' => '验证码必须为整数',
            'code.between' => '验证码长度应在4-6位',
            'uuid.required' => '请传入设备唯一识别码 ',
            'uuid.string' => '设备唯一识别码必须为字符串'
        ];

        // 根据配置选择规则组成
        $rule = array_intersect_key($rule, array_flip($ruleArr));

        $validate = Validator::make($data, $rule, $message);
        if($validate->fails()){
            throw new \Exception($validate->errors()->first());
        }
        return true;
    }


    /***RPC 获取用户信息***/

    public function getInfo(Request $request)
    {
        $userId = $request->input('user_id');

        try {
            $data = $this->getUserInfoHandle($userId);
            unset($data['password']);
            return $this->success('获取成功', $data);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

    /**
     * User: zhangchaoqun
     * DateTime: 2021/6/15 5:40 下午
     * Describe: 根据手机号登录
     * Version: V1.0
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function loginByPhone(Request $request)
    {

        // 接收参数
        $params = $request->input();
        // 参数字段
        $fields = ['phone', 'code', 'uuid'];
        //请求CRM RPC接口   rcp添加 im和用户关系

        try {
            // 校验参数
            $this->validate($params, $fields);

            // 请求CRM RPC接口登录
            $data = $this->userLoginByPhone($params['phone']);


            // 生成登录TOKEN
            $userTokenService = new UserTokenService();
            $token = $userTokenService->getToken(['ttl' => 0, 'source' => 'user', 'id' => $data['id']]);


            // 请求CRM RPC接口 查询用户状态
            //$userdata = $this->userAuth($data['id']);
           //dd($userdata);
            //$InfoHandle = $this->getUserInfoHandle($data['id']);
/*            if(empty($userdata)){
               //操作腾讯iM 用户导入
                $imServer =new ImService();
                $imparams=[
                    'id'    =>$data['id'],
                    'avatar'=> $data['avatar'],
                    'realname'=> $data['realname'],
                    ];
                $imServer->useradd($imparams,$type=1);
                //请求CRM RPC接口   rcp添加 im和用户关系
                $this->userAuthAdd($data['id']);
            }*/

            return $this->success('登录成功', [
                'uuid' => md5('doctor_'.$data['id']),
                'datsa' =>$data,
                'token' => $token
            ]);
        } catch (\Exception $exception) {
            return $this->error($exception->getMessage());
        }
    }

}
